import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class PageViewBuilderFullPagePage extends StatefulWidget {
  const PageViewBuilderFullPagePage({super.key});

  @override
  State<PageViewBuilderFullPagePage> createState() =>
      _PageViewBuilderFullPagePageState();
}

class _PageViewBuilderFullPagePageState
    extends State<PageViewBuilderFullPagePage> {
  List<Widget> list = [];

  @override
  void initState() {
    super.initState();
    for (var i = 0; i < 10; i++) {
      list.add(Center(
        child: Text(
          "第${i + 1}屏",
          style: const TextStyle(
            fontSize: 60,
          ),
        ),
      ));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("PageView 无限加载 演示"),
      ),
      body: PageView(
        scrollDirection: Axis.vertical,
        onPageChanged: (index) {
          if (kDebugMode) {
            print(index);
          }
          if (index + 2 >= list.length) {
            setState(() {
              for (var i = 0; i < 10; i++) {
                list.add(Center(
                  child: Text(
                    "第${list.length + 1}屏",
                    style: const TextStyle(
                      fontSize: 60,
                    ),
                  ),
                ));
              }
            });
          }
        },
        children: list,
      ),
    );
  }
}
